﻿
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace VkontakteButton
{
    /// <summary>
    /// Description of VkontakteButton.
    /// </summary>
    public partial class VkontakteButton : UserControl, IButtonControl
    {
        static Color backcolor = Color.FromArgb(103, 143, 183);
        static Color downcolor = Color.FromArgb(22, 51, 80);
        static Color disabledcolor = Color.FromArgb(10, 51, 80);

        static Pen pen = new Pen(Color.Gray, 1);
        private bool focused = false;
        private DialogResult myDialogResult;

        Brush brush = new SolidBrush(backcolor);
        Font font = new Font("Tahoma", 8, FontStyle.Regular);

        /// <summary>
        /// blah-lbah
        /// </summary>
        public string CurrentText
        {
            get;
            set;
        }

        Rectangle border;
        const int height = 22;
        public VkontakteButton()
        {
            CurrentText = "VkontakteButton";
            border = new Rectangle(0, (this.Height - height) / 2, this.Width - 2, height);
            pen.DashStyle = DashStyle.Dot;

            InitializeComponent();
        }

        void VkontakteButtonPaint(object sender, PaintEventArgs e)
        {
            if (!this.Enabled)
            {
                this.brush = new SolidBrush(disabledcolor);
            }

            GraphicsPath path = RoundedRectangle.Create(border, 3);
            e.Graphics.FillPath(brush, path);
            e.Graphics.DrawPath(Pens.Black, path);

            var size = TextRenderer.MeasureText(CurrentText, font);
            e.Graphics.DrawString(CurrentText, font, Brushes.White, border.X + (border.Width - size.Width) / 2, border.Top + 4);
            if (focused)
            {
                e.Graphics.DrawRectangle(pen, border);
            }
        }

        void VkontakteButtonResize(object sender, EventArgs e)
        {
            border = new Rectangle(0, (this.Height - height) / 2, this.Width - 2, height);
        }

        void VkontakteButtonMouseDown(object sender, MouseEventArgs e)
        {
            if (border.Contains(e.X, e.Y))
            {
                PaintDown();
            }
        }

        void PaintDown()
        {
            if (border.X == 0)
            {
                brush = new SolidBrush(downcolor);
                border.X += 1;
                border.Y += 1;
                this.Invalidate();
            }
        }

        void VkontakteButtonMouseMove(object sender, MouseEventArgs e)
        {
            if (border.Contains(e.X, e.Y))
            {
                Cursor = Cursors.Hand;
            }
            else
            {
                Cursor = Cursors.Arrow;
            }
        }

        void VkontakteButtonMouseUp(object sender, MouseEventArgs e)
        {
            PaintUp();
        }

        void PaintUp()
        {
            brush = new SolidBrush(backcolor);
            border.X = 0;
            border.Y = (this.Height - height) / 2;

            this.Invalidate();
        }

        void VkontakteButtonEnter(object sender, EventArgs e)
        {
            focused = true;
            this.Invalidate();
        }

        void VkontakteButtonLeave(object sender, EventArgs e)
        {
            focused = false;
            this.Invalidate();
        }

        public DialogResult DialogResult
        {
            get
            {
                return this.myDialogResult;
            }
            set
            {
                if (Enum.IsDefined(typeof(DialogResult), value))
                {
                    this.myDialogResult = value;
                }
            }
        }

        public void NotifyDefault(bool value)
        {
            //throw new NotImplementedException();
        }

        public virtual void PerformClick()
        {
            this.OnClick(null);
            //throw new NotImplementedException();
        }

        private void VkontakteButton_EnabledChanged(object sender, EventArgs e)
        {
            if (this.Enabled)
            {
                this.brush = new SolidBrush(backcolor);
            }
            this.Invalidate();
        }
    }
}
